/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.pajamas.starter.core.util;

import org.apache.commons.lang3.StringUtils;
import org.pajamas.starter.integration.module.context.PajamasPropSrcConstants;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.ClassUtils;

import java.io.File;
import java.io.IOException;

import lombok.Setter;

/**
 * @author william
 * @since 2024/1/16
 */

public final class ClasspathUtils {
    private static final ResourcePatternResolver RESOURCE_PATTERN_RESOLVER;
    private static final String RESOURCE_SEPARATOR;
    private static final String CLASSPATH_ALL;
    private static final String CLASSPATH_CUR;
    private static final String CLASS_PATTERN;
    private static final String CLASSES_PATTERN;

    @Setter
    private static PropertyResolver PROPERTY_RESOLVER;

    static {
        RESOURCE_PATTERN_RESOLVER = new PathMatchingResourcePatternResolver();
        RESOURCE_SEPARATOR = File.separator;
        CLASSPATH_ALL = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX;
        CLASSPATH_CUR = "classpath:";
        CLASS_PATTERN = "*.class";
        CLASSES_PATTERN = StringUtils.join(RESOURCE_SEPARATOR, "**", RESOURCE_SEPARATOR, CLASS_PATTERN);
    }

    private ClasspathUtils() {
    }

    public static String getCurrentBasePackage() {
        return PROPERTY_RESOLVER.getProperty(PajamasPropSrcConstants.BASE_PACKAGE_APP, String.class);
    }

    public static String toResourcePath(String basePackage, boolean coverAllClasspath) {
        if (StringUtils.isBlank(basePackage)) {
            return null;
        }
        String classpath = coverAllClasspath ? CLASSPATH_ALL : CLASSPATH_CUR;
        String resourcePath = ClassUtils.convertClassNameToResourcePath(basePackage);
        return StringUtils.join(classpath, resourcePath, CLASSES_PATTERN);
    }

    public static Resource[] scanResources(String dirPath, String filePattern, boolean coverAllClasspath) throws IOException {
        String classpath = coverAllClasspath ? CLASSPATH_ALL : CLASSPATH_CUR;
        String classpathLocation = StringUtils.join(classpath, dirPath, filePattern);
        return RESOURCE_PATTERN_RESOLVER.getResources(classpathLocation);
    }
}